package jp.co.sharp.android.parents.kidsguard.utils;

import android.app.Activity;
import android.app.Application;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.util.Log;

import com.android.bluetooth.parentble.ParentBleTransferCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import jp.co.sharp.android.parents.kidsguard.contants.Constants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestReceiverConstants;
import jp.co.sharp.android.parents.kidsguard.dto.BluetoothReceiverDto;

/**
 *
 */

public class ParentBleTransferCallbackImpl implements ParentBleTransferCallback {

    private Application mApplication;

    public ParentBleTransferCallbackImpl(Application application){
        this.mApplication = application;
    }
    //发现蓝牙列表的方法
    @Override
    public void onLeScanStateChanged(BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecord) {

        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_SEARCH);
        intent.putExtra(RequestReceiverConstants.RECEIVER_KEY, bluetoothDevice);
        intent.putExtra(RequestReceiverConstants.RECEIVER_MAC_ID_KEY, scanRecord);
        mApplication.sendBroadcast(intent);

    }
    //蓝牙连接的方法
    @Override
    public void onLeConnetStateChanged(String s, int i, int i1) {
        try {

            BluetoothReceiverDto bluetoothReceiverDto = new BluetoothReceiverDto();
            bluetoothReceiverDto.setAddress(s);
            bluetoothReceiverDto.setStatus(i);
            bluetoothReceiverDto.setReson(i1);
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_CONNECT_STATE_CHANGED);
            intent.putExtra(RequestReceiverConstants.RECEIVER_KEY, bluetoothReceiverDto);
            mApplication.sendBroadcast(intent);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //发送数据的时候 kid手机收到数据的心跳方法
    @Override
    public void onLeWriteDataChanged(String s, boolean b) {
        try {

            // 心跳
            BluetoothReceiverDto bluetoothReceiverDto = new BluetoothReceiverDto();
            bluetoothReceiverDto.setAddress(s);
            bluetoothReceiverDto.setFinished(b);
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_WRITE_DATA_CHANGED);
            intent.putExtra(RequestReceiverConstants.RECEIVER_KEY, bluetoothReceiverDto);
            mApplication.sendBroadcast(intent);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //kid手机返回数据的方法
    @Override
    public void onLeListenerDataReceiveChanged(String s, String s1) {
        try {
            // 返回值
            BluetoothReceiverDto bluetoothReceiverDto = new BluetoothReceiverDto();
            bluetoothReceiverDto.setAddress(s);
            bluetoothReceiverDto.setRetStr(s1);
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED);
            intent.putExtra(RequestReceiverConstants.RECEIVER_KEY, bluetoothReceiverDto);
            mApplication.sendBroadcast(intent);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //图片传输 返回百分比的方法
    @Override
    public void onLeWriteFileProcessChanged(String s, float v) {
        try {
            JSONObject resStr = new JSONObject();
            resStr.put("address", s);
            resStr.put("imgSendProcess", v);
            Log.v("imgSendProcess", String.valueOf(v));
            if(v == 100f){
                Log.v("imgSend finish", String.valueOf(v));
            }
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_WRITE_FILE_CHANGED);
            intent.putExtra("imgSendProcess", v);
            mApplication.sendBroadcast(intent);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //发现服务的方法 暂时不使用 所有 这个地方的 pwebview 和 callbackid 不适用
    @Override
    public void onServiceDiscoveryChanged(String s, boolean b) {
        try {

            // 小手机自动回复
            BluetoothReceiverDto bluetoothReceiverDto = new BluetoothReceiverDto();
            bluetoothReceiverDto.setAddress(s);
            bluetoothReceiverDto.setDiscoveryState(b);
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_SERVICE_DISCOVERY_CHANGED);
            intent.putExtra(RequestReceiverConstants.RECEIVER_KEY, bluetoothReceiverDto);
            mApplication.sendBroadcast(intent);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onLeHandlerException(int i) {
        try {
            JSONObject handlerException = new JSONObject();
            handlerException.put("isExcepted", true);
            handlerException.put("method", "exception");
            handlerException.put("num", i);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLeUnPairedChanged(String s, boolean b) {
        Log.v("onLeUnPairedChanged", "S:" + s);
        Log.v("onLeUnPairedChanged", "B:" + String.valueOf(b));
    }

    @Override
    public void onLeCancelFileTransferChange(String s, boolean b) {
        Log.v("onLeCancelFileTransfer", "S:" + s);
        Log.v("onLeCancelFileTransfer", "B:" + String.valueOf(b));
        JSONObject resultObject = new JSONObject();
        try {
            resultObject.put("state", b);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
};
